package com.qxzl.filter;

public class BusinessException  extends RuntimeException{

    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = 4156090916459941859L;

    /**
     * 业务异常信息
     */
    private String code = "";
    /**
     * 异常附加信息
     */
    private String msg = "";

    public BusinessException() {
        super();
    }

    public BusinessException(String code) {
        super(code);
    }

    /**
     * @Title: BusinessException
     * @Description: 用于业务逻辑层——业务错误信息 (不带自定义附件异常信息)
     * @param @param e 堆栈跟踪详细信息
     * @param @param code 异常编号
     */
    public BusinessException(Exception e, String code) {
        super(e);
        this.code = code;
    }

    /**
     * @Title: BusinessException
     * @Description: 用于业务逻辑层——业务错误信息 (不带详细堆栈跟踪信息)
     * @param @param code 异常编号
     * @param @param msg 自定义异常附加内容
     */
    public BusinessException(String code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    /**
     * @Title: BusinessException
     * @Description: 用于业务逻辑层——业务错误信息 (带详细堆栈跟踪信息)
     * @param @param e 堆栈跟踪详细信息
     * @param @param code 异常编号
     * @param @param msg 自定义异常附加内容
     */
    public BusinessException(Exception e, String code, String msg) {
        super(e);
        this.code = code;
        this.msg = msg;
    }

    public String getExpMessage() {
        String message = super.getMessage();
        message = "[" + message + "]";
        Throwable cause = getCause();
        if (cause != null) {
            StackTraceElement[] stes = cause.getStackTrace();
            if (stes != null) {
                for (int i = 0; i < stes.length; i++) {
                    StackTraceElement e = stes[i];
                    String className = e.getClassName();
                    if (className.contains("rainbow")) {
                        String fileName = e.getFileName();
                        int lineNumber = e.getLineNumber();
                        String methodName = e.getMethodName();
                        String traceStr = className + "." + methodName + "("
                                + fileName + ":" + lineNumber + ")";
                        message = message + "[" + traceStr + "]";
                    }
                }
            }
        }
        return message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
